import json
from django_redis import get_redis_connection
from utils.cookiesecret import CookieSecret


def merge_cart_cookie_to_redis(request, response):
    cookie_str = request.COOKIES.get('carts')
    if cookie_str is not None:
        cookie_dict = CookieSecret.loads(cookie_str)
        client = get_redis_connection('carts')
        for sku_id in cookie_dict:
            client.hset(request.user.id, sku_id, json.dumps(cookie_dict[sku_id]))
        response.delete_cookie('carts')
